﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.Model;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class ApplicantController : Controller
    {
        private IApplicantService _applicantService;
        public ApplicantController(IApplicantService applicantService)
        {
            _applicantService = applicantService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        [HttpGet]
        public IActionResult GetApplicantList(int pageIndex = 1)
        {
            IList<ApplicantInf> applicantInfs = new List<ApplicantInf>();
            applicantInfs = _applicantService.GetAppInf(pageIndex);            
            int pageCount = (int)Math.Ceiling((double)_applicantService.GetAppInfCount() / 20);
            ViewData["PageCount"] = pageCount;
            if (pageCount == 0)
            {
                ViewData["PageIndex"] = 0;
            }
            else
            {
                ViewData["PageIndex"] = pageIndex;
            }
            ViewData["Title"] = "学生管理";
            return View(applicantInfs);
        }

        [AllowAnonymous]
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public IActionResult Login([FromForm]Applicant applicant)
        {
            var appli = _applicantService.Login(applicant.Email, applicant.PassWord);
            if (appli == null)
            {
                return Json(AjaxResult.ReturnMessage(0, "账号或密码错误"));
            }
            else
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.Sid, applicant.Id.ToString()));
                identity.AddClaim(new Claim(ClaimTypes.Name, appli.Email));
                identity.AddClaim(new Claim(ClaimTypes.Role, appli.Role));
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, appli.Email + appli.Role));
                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
                return Json(AjaxResult.ReturnMessage(1, "登录成功"));
            }
        }

        [HttpGet]
        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return RedirectToAction("Login", "Home");
        }

        [HttpGet]
        public IActionResult Denied()
        {
            return View();
        }

        [HttpGet]
        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Register([FromForm]Applicant user)
        {           
            user.Role = "Applicant";            
            var Applicant = _applicantService.GetApplicant(user.Email);
            if (Applicant == null)
            {
                _applicantService.Register(user);
                return Json(AjaxResult.ReturnMessage(1, "注册成功"));
            }
            else
                return Json(AjaxResult.ReturnMessage(0, "注册失败，邮箱被占用"));
        }
    }
}
